<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">新宇工资后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="userName">
                    <el-input v-model="param.userName" placeholder="请输入账号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="verCode">
                    <el-input style="width: 150px" placeholder="输入验证码" v-model="param.verCode"></el-input>
                    <el-image style="width: 100px; height: 30px" :src="src" fit="fit" class="ca" @click="getCaptcha()"></el-image>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="default" @click="resetForm()">重置</el-button>
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function () {
        return {
            src: '',
            param: {
                userName: '',
                password: '',
                key: '',
                verCode: ''
            },
            rules: {
                userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                verCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
            }
        };
    },
    created() {
        this.getCaptcha();
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(async (valid) => {
                if (valid) {
                    //后端请求校验
                    var d = await this.$http.post('/login', this.param);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        window.sessionStorage.setItem('token', JSON.stringify(d.data));
                        this.getUser(d.data.tid);
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                    this.$message.error('请输入账号和密码或者验证码');
                    console.log('错误提交!!');
                    return false;
                }
            });
        },

        async getCaptcha() {
            var d = await this.$http.get('/captcha');
            if (d.code == 200) {
                this.param.key = d.message;
                this.src = d.data;
                console.log(this.src);
            }
        },

        async getUser(tid) {
            var d = await this.$http.get('/userTid/' + tid);
            if (d.code == 200) {
                window.sessionStorage.setItem('user', JSON.stringify(d.data));
                this.savePermisison(d.data.roles);
                this.$router.push('/');
            }
        },
        savePermisison(roles) {
            let c = [];
            for (let i = 0; i < roles.length; i++) {
                c.push(roles[i].permissions);
            }
            this.permissions = this.initPers(this.unique(c));
            window.sessionStorage.setItem('pers', JSON.stringify(this.permissions));
        },
        resetForm() {
            this.$refs.login.resetFields();
        },
        unique(menus) {
            let result = {};
            let finalResult = [];
            for (let i = 0; i < menus.length; i++) {
                result[menus[i].name] = menus[i];
            }
            for (var item in result) {
                finalResult.push(result[item]);
            }
            return finalResult[0];
        },
        initPers(pers) {
            let p = [];
            for (let i = 0; i < pers.length; i++) {
                p.push(pers[i].name);
            }
            return p;
        }
    }
};
</script>

<style scoped>
.ca {
    position: relative;
    top: 10px;

    margin-left: 20px;
}
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 25px;
    color: #fff;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(0, 0, 0, 0);
    overflow: hidden;
}
.ms-content {
    padding: 20px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 48%;
    height: 36px;
    margin-bottom: 10px;
}
</style>